package com.logicalthinking.socialmall.service.impl;

import com.github.chenlijia1111.commonModule.utils.SpringUtil;
import com.github.chenlijia1111.utils.common.Result;
import com.github.chenlijia1111.utils.core.PropertyCheckUtil;
import com.github.chenlijia1111.utils.core.StringUtils;
import com.github.chenlijia1111.utils.oauth.jwt.JWTUtil;
import com.logicalthinking.socialmall.common.pojo.Constants;
import com.logicalthinking.socialmall.common.vo.requestVo.admin.AdminListQueryParams;
import com.logicalthinking.socialmall.common.vo.responseVo.admin.AdminVo;
import com.logicalthinking.socialmall.dao.AdminMapper;
import com.logicalthinking.socialmall.entity.Admin;
import com.logicalthinking.socialmall.service.AdminServiceI;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * 管理员用户
 *
 * @author chenLiJia
 * @since 2020-02-17 14:43:59
 **/
@Service
public class AdminServiceImpl implements AdminServiceI {


    @Resource
    private AdminMapper adminMapper;


    /**
     * 添加
     *
     * @param params 添加参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public Result add(Admin params) {

        int i = adminMapper.insertSelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 编辑
     *
     * @param params 编辑参数
     * @return com.github.chenlijia1111.utils.common.Result
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public Result update(Admin params) {

        int i = adminMapper.updateByPrimaryKeySelective(params);
        return i > 0 ? Result.success("操作成功") : Result.failure("操作失败");
    }

    /**
     * 列表查询后台用户
     *
     * @param params
     * @return
     */
    @Override
    public List<AdminVo> listPage(AdminListQueryParams params) {
        params = PropertyCheckUtil.transferObjectNotNull(params);
        List<AdminVo> adminVos = adminMapper.listPage(params);
        return adminVos;
    }

    /**
     * 查询当前用户
     *
     * @return
     */
    @Override
    public Optional<Admin> currentAdmin() {

        HttpServletRequest request = SpringUtil.getCurrentRequest();
        String token = request.getHeader(Constants.TOKEN);
        if (StringUtils.isNotEmpty(token)) {
            //解析token
            try {
                Claims claims = JWTUtil.parseJWT(token, Constants.JWT_SIGN_KEY);
                String id = claims.getId();
                if (StringUtils.isInt(id)) {
                    Admin admin = adminMapper.selectByPrimaryKey(Integer.valueOf(id));
                    return Optional.ofNullable(admin);
                }
            } catch (Exception e) {
                e.printStackTrace();
                //如果抛出异常,解析失败
            }

        }
        return Optional.empty();
    }

    /**
     * 条件查询
     *
     * @param condition
     * @return
     * @author chenLiJia
     * @since 2020-02-17 14:43:59
     **/
    @Override
    public List<Admin> listByCondition(Admin condition) {

        PropertyCheckUtil.transferObjectNotNull(condition, true);
        return adminMapper.select(condition);
    }


}
